#include "StdAfx.h"
#include "Square.h"
#include "LocalPane.h"

CSquare::CSquare(CLocalPane* pSquarePane, CPoint pntLeftTop, UINT nSideWidth, bool bFlag, COLORREF color)
	: m_pSquarePane(pSquarePane)
	, m_pntLeftTop(pntLeftTop)
	, m_nSideWidth(nSideWidth)
	, m_bDrawFlag(bFlag)
	, m_SquareColor(color)
{
	// Rect
	m_rectSquare.left = m_pntLeftTop.x;
	m_rectSquare.top = m_pntLeftTop.y;
	m_rectSquare.right = m_pntLeftTop.x + m_nSideWidth;
	m_rectSquare.bottom = m_pntLeftTop.y + m_nSideWidth;
}

CSquare::~CSquare(void)
{
}

void CSquare::Draw(CDC* pDC)
{
	if (m_bDrawFlag)
	{
		pDC->Rectangle(m_pntLeftTop.x, m_pntLeftTop.y, m_pntLeftTop.x + m_nSideWidth, m_pntLeftTop.y + m_nSideWidth);
	}
}

void CSquare::Draw(CDC* pDC, CPoint firstPoint, double fScale)
{
}

void CSquare::Fill(CDC* pDC)
{
	if (m_bDrawFlag)
	{
		// Draw outline rect
		CPen pen, *pOldPen;
		pen.CreatePen(PS_SOLID, 2, RGB(255,255,255));
		pOldPen = pDC->SelectObject(&pen);
		pDC->Rectangle(&m_rectSquare);
		pDC->SelectObject(pOldPen);
	
		// Fill rect
		CBrush brush, *pOldBrush;
		brush.CreateSolidBrush(m_SquareColor);
		pOldBrush = pDC->SelectObject(&brush);

		CRect fillRect;
		fillRect.left = m_rectSquare.left + 1;
		fillRect.right = m_rectSquare.right - 1;
		fillRect.top = m_rectSquare.top + 1;
		fillRect.bottom = m_rectSquare.bottom - 1;

		pDC->FillRect(&fillRect, &brush);

		pDC->SelectObject(pOldBrush);
	}
}

void CSquare::Fill(CDC* pDC, CPoint firstPoint, double fScale)
{
	if (m_bDrawFlag)
	{
		// Calc rectangle
		CRect drawRect;
		drawRect.left = firstPoint.x;
		drawRect.top = firstPoint.y;
		drawRect.right = drawRect.left + fScale * m_nSideWidth;
		drawRect.bottom = drawRect.top + fScale * m_nSideWidth;

		// Draw outline rect
		CPen pen, *pOldPen;
		pen.CreatePen(PS_SOLID, 2, RGB(255,255,255));
		pOldPen = pDC->SelectObject(&pen);

		pDC->Rectangle(&drawRect);

		pDC->SelectObject(pOldPen);
	
		// Fill rect
		CBrush brush, *pOldBrush;
		brush.CreateSolidBrush(m_SquareColor);
		pOldBrush = pDC->SelectObject(&brush);

		CRect fillRect;
		fillRect.left = drawRect.left + 1;
		fillRect.right = drawRect.right - 1;
		fillRect.top = drawRect.top + 1;
		fillRect.bottom = drawRect.bottom - 1;

		pDC->FillRect(&fillRect, &brush);

		pDC->SelectObject(pOldBrush);
	}
}

//---------------------------------------------------------------------------------

void CSquare::GoDown()
{
	// Update first point
	m_pntLeftTop.y += m_nSideWidth;

	// Update rect
	m_rectSquare.top += m_nSideWidth;
	m_rectSquare.bottom += m_nSideWidth;
}

void CSquare::GoLeft()
{
	// Update first point
	m_pntLeftTop.x -= m_nSideWidth;

	// Update rect
	m_rectSquare.left -= m_nSideWidth;
	m_rectSquare.right -= m_nSideWidth;
}

void CSquare::GoRight()
{
	// Update first point
	m_pntLeftTop.x += m_nSideWidth;

	// Update rect
	m_rectSquare.left += m_nSideWidth;
	m_rectSquare.right += m_nSideWidth;
}

//---------------------------------------------------------------------------------

bool CSquare::operator==(const CSquare& square)
{
	if (square.GetLeftTop().x == m_pntLeftTop.x && 
		square.GetLeftTop().y == m_pntLeftTop.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//---------------------------------------------------------------------------------

UINT CSquare::GetSideWidth() const 
{ 
	return m_nSideWidth; 
}

void CSquare::SetSideWidth(UINT nSideWidth) 
{ 
	m_nSideWidth = nSideWidth; 
}

CPoint CSquare::GetLeftTop() const 
{ 
	return m_pntLeftTop; 
}

void CSquare::SetLeftTop(const CPoint& point) 
{ 
	m_pntLeftTop = point;

    CPoint pntBottomRight;
    pntBottomRight.x = m_pntLeftTop.x + m_nSideWidth;
    pntBottomRight.y = m_pntLeftTop.y + m_nSideWidth;

    m_rectSquare.SetRect(m_pntLeftTop, pntBottomRight);
}

bool CSquare::GetFlag() const 
{ 
	return m_bDrawFlag; 
}

void CSquare::SetFlag(bool bFlag) 
{ 
	m_bDrawFlag = bFlag; 
}

COLORREF CSquare::GetColor() const 
{ 
	return m_SquareColor; 
}

void CSquare::SetColor(COLORREF color)
{
	m_SquareColor = color;
}

CRect CSquare::GetRect() const
{
	return m_rectSquare;
}

void CSquare::SetRect(const CRect& rect)
{
    m_rectSquare.SetRect(rect.TopLeft(), rect.BottomRight());
    
    m_pntLeftTop = m_rectSquare.TopLeft();
}

CPoint CSquare::GetRightBottom() const
{
	CPoint point;

	point.x = m_pntLeftTop.x + m_nSideWidth;
	point.y = m_pntLeftTop.y + m_nSideWidth;

	return point;
}
